package com.zjitc.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Author: ${赵诚挚}
 * @Date: 2024/6/21 10:54
 */
//@Configuration设置
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        /*创建url的cors配置地址对象*/
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        /*创建Cors配置对象*/
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 设置访问源地址
        corsConfiguration.addAllowedOrigin("*");
        // 设置访问源请求头
        corsConfiguration.addAllowedHeader("*");
        // 设置访问源请求方法
        corsConfiguration.addAllowedMethod("*");
        // 设置跨域
        source.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsFilter(source);
    }
}
